package mango.web.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.datepicker.client.CalendarUtil;

import mango.web.client.admin.AdminViewI;
import mango.web.client.admin.AdminViewIAsync;
import mango.web.client.admin.ContactVo;
import mango.web.client.admin.EmailContentVo;
import mango.web.client.admin.EmailVo;
import mango.web.client.admin.FlowerChartVo;
import mango.web.client.admin.LookupTVo;
import mango.web.client.admin.MemberPropertyVo;
import mango.web.client.admin.MemberVo;
import mango.web.client.admin.OrchardVo;
import mango.web.client.admin.RegionVo;
import mango.web.client.admin.SmsContentVo;
import mango.web.client.admin.StateVo;
import mango.web.client.admin.TemperatureUrlVo;
import mango.web.client.admin.VarietyVo;

public class C extends Vo {
	public static String _version = "1.0.3";
	public static AdminViewIAsync adminViewS;

	public Date date = new Date();
	public static Date _date;

	public Integer flowerYear;
	public static Integer _flowerYear;

	public MemberVo memberVo = new MemberVo();
	public static MemberVo _memberVo;

	public List<VarietyVo> varietyList = new ArrayList<VarietyVo>();
	public static List<VarietyVo> _varietyVoList;

	public List<RegionVo> regionList = new ArrayList<RegionVo>();
	public static List<RegionVo> _regionVoList;

	public List<LookupTVo> lookupTList = new ArrayList<LookupTVo>();
	public static List<LookupTVo> _lookupTVoList;

	public Map<String, TemperatureUrlVo> temperatureUrlVoMap = new HashMap<String, TemperatureUrlVo>();
	public static Map<String, TemperatureUrlVo> _temperatureUrlVoMap;

	public MemberPropertyVo memberPropertyVo = new MemberPropertyVo();
	public static MemberPropertyVo _memberPropertyVo;

	public List<EmailVo> emailList = new ArrayList<EmailVo>();
	public static List<EmailVo> _emailVoList = new ArrayList<EmailVo>();

	public List<EmailContentVo> emailContentList = new ArrayList<EmailContentVo>();
	public static List<EmailContentVo> _emailContentList = new ArrayList<EmailContentVo>();

	public List<SmsContentVo> smsContentList = new ArrayList<SmsContentVo>();
	public static List<SmsContentVo> _smsContentList = new ArrayList<SmsContentVo>();

	public static Map<String, Map<String, FlowerChartVo>> _flowerChartList;

	public static List<StateVo> _stateVoList;

	public static List<String> _contactIdList;
	public static List<ContactVo> _searchContactList;

	public static MemberVo _testMember;
	public static ContactVo _testContact;
	public static OrchardVo _testOrchard;

	public static void set(C c) {
		C._date = c.date;
		C._flowerYear = c.flowerYear;

		C._memberVo = c.memberVo;
		C._varietyVoList = c.varietyList;
		C._regionVoList = c.regionList;
		C._memberPropertyVo = c.memberPropertyVo;
		C._emailVoList = c.emailList;
		C._emailContentList = c.emailContentList;
		C._smsContentList = c.smsContentList;
		C._lookupTVoList = c.lookupTList;
		C._temperatureUrlVoMap = c.temperatureUrlVoMap;
	}

	public static void init() {
		C.adminViewS = GWT.create(AdminViewI.class);

		C._stateVoList = new ArrayList<StateVo>();
		C._stateVoList.add(new StateVo().init("QLD"));
		C._stateVoList.add(new StateVo().init("NT"));
		C._stateVoList.add(new StateVo().init("WA"));
		C._stateVoList.add(new StateVo().init("NSW"));
		C._stateVoList.add(new StateVo().init("VIC"));
		C._stateVoList.add(new StateVo().init("SA"));
		C._stateVoList.add(new StateVo().init("ACT"));
		C._stateVoList.add(new StateVo().init("TAS"));

		C._testMember = new MemberVo();
		C._testMember.memberId = -111;
		C._testMember.businessName = "businessName";
		C._testMember.abn = "abn";
		C._testMember.businessType = "businessType";
		C._testMember.other = "other";
		C._testMember.membershipType = "membershipType";
		C._testMember.financialMember = "financialMember";
		C._testMember.annualReport = "annualReport";
		C._testMember.authorisedContact = -1;
		C._testMember.marketingGroupUsed = "marketingGroupUsed";
		C._testMember.packsOwnProduce = "packsOwnProduce";
		C._testMember.marketGroup = "marketGroup";
		C._testMember.brand = "brand";
		C._testMember.status = "status";

		C._testContact = new ContactVo();
		C._testContact.contactId = -11;
		C._testContact.memberId = -111;
		C._testContact.firstname = "firstname";
		C._testContact.surname = "surname";
		C._testContact.BHPhone = "BHPhone";
		C._testContact.AHPhone = "AHPhone";
		C._testContact.fax = "fax";
		C._testContact.identifier = "identifier";
		C._testContact.mobile = "mobile";
		C._testContact.email = "email";
		C._testContact.service = "service";
		C._testContact.businessStreet = "businessStreet";
		C._testContact.businessCity = "businessCity";
		C._testContact.businessState = "businessState";
		C._testContact.businessCountry = "businessCountry";
		C._testContact.businessPostCode = "businessPostCode";
		C._testContact.postalStreet = "postalStreet";
		C._testContact.postalCity = "postalCity";
		C._testContact.postalState = "postalState";
		C._testContact.postalCountry = "postalCountry";
		C._testContact.postalPostCode = "postalPostCode";
		C._testContact.mailMemberId = -111;
		C._testContact.mailContactId = -11;
		C._testContact.emailFaxContactId = -11;
		C._testContact.status = "status";

		C._testOrchard = new OrchardVo();
		C._testOrchard.orchardId = -1;
		C._testOrchard.memberId = -111;
		C._testOrchard.physicalLocation = "physicalLocation";
		C._testOrchard.postCode = "postCode";
		C._testOrchard.state = "state";
		C._testOrchard.emailContactId = "-11";
		C._testOrchard.status = "status";
	}

	public static String getStatus(String status) {
		if ("".equals(status)) return "active";
		return status;
	}

	public static List<String> getStartFlowerDateList(String format) {
		List<String> list = new ArrayList<String>();
		DateTimeFormat dtf = DateTimeFormat.getFormat(format);
		for (int j = 0; j < C._memberPropertyVo.columns; j++) {
			Date start = new Date(C._flowerYear - 1900, C._memberPropertyVo.startMonth - 1, C._memberPropertyVo.startDay);
			CalendarUtil.addDaysToDate(start, j * C._memberPropertyVo.interval);
			list.add(dtf.format(start));
		}
		return list;
	}

	public static List<String> getEndFlowerDateList(String format) {
		List<String> list = new ArrayList<String>();
		DateTimeFormat dtf = DateTimeFormat.getFormat(format);
		for (int j = 0; j < C._memberPropertyVo.columns; j++) {
			Date end = new Date(C._flowerYear - 1900, C._memberPropertyVo.startMonth - 1, C._memberPropertyVo.startDay);
			CalendarUtil.addDaysToDate(end, (j + 1) * C._memberPropertyVo.interval - 1);
			list.add(dtf.format(end));
		}
		return list;
	}

	public static Map<String, String> getVarietyMap() {
		Map<String, String> varietyMap = new HashMap<String, String>();
		for (int i = 0; i < C._varietyVoList.size(); i++) {
			VarietyVo variety = C._varietyVoList.get(i);
			varietyMap.put(variety.varietyId + "", variety.varietyName);
		}
		return varietyMap;
	}

	public static Map<String, String> getRegionMap() {
		Map<String, String> regionMap = new HashMap<String, String>();
		for (int i = 0; i < C._regionVoList.size(); i++) {
			RegionVo region = C._regionVoList.get(i);
			regionMap.put(region.regionId + "", region.regionName);
		}
		return regionMap;
	}

	public static Map<String, String> getStateMap() {
		Map<String, String> stateMap = new HashMap<String, String>();
		for (int i = 0; i < C._stateVoList.size(); i++) {
			StateVo state = C._stateVoList.get(i);
			stateMap.put(state.stateName + "", state.stateName);
		}
		return stateMap;
	}

	public static RegionVo getRegionByPostCode(String postCode) {
		if ("".equals(postCode)) return null;
		for (int i = 0; i < C._regionVoList.size(); i++) {
			RegionVo region = C._regionVoList.get(i);
			if (region.inStringList(region.postCodes, postCode)) return region;
		}
		return null;
	}

	///////////////
	public static int getYear() {
		return 1900 + C._date.getYear();
	}

	public static String getFirstLine(String str) {
		String result = str;
		int index = str.indexOf("\n");
		if (index != -1) {
			result = str.substring(0, str.indexOf("\n"));
		}
		return result;
	}

	public static String decode(String template) {
		String string = template;
		string = string.replaceAll("%7B", "{");
		string = string.replaceAll("%7D", "}");
		return string;
	}

	public static String templateToString(String template, MemberVo member, ContactVo contact, OrchardVo orchard) {
		String string = template;
		if (member != null) {
			string = string.replaceAll("\\$\\{member.memberId\\}", member.memberId + "");
			string = string.replaceAll("\\$\\{member.businessName\\}", member.businessName);
			string = string.replaceAll("\\$\\{member.abn\\}", member.abn);
			string = string.replaceAll("\\$\\{member.businessType\\}", member.businessType);
			string = string.replaceAll("\\$\\{member.other\\}", member.other);
			string = string.replaceAll("\\$\\{member.membershipType\\}", member.membershipType);
			string = string.replaceAll("\\$\\{member.financialMember\\}", member.financialMember);
			string = string.replaceAll("\\$\\{member.annualReport\\}", member.annualReport);
			string = string.replaceAll("\\$\\{member.authorisedContact\\}", member.authorisedContact + "");
			string = string.replaceAll("\\$\\{member.marketingGroupUsed\\}", member.marketingGroupUsed);
			string = string.replaceAll("\\$\\{member.packsOwnProduce\\}", member.packsOwnProduce);
			string = string.replaceAll("\\$\\{member.marketGroup\\}", member.marketGroup);
			string = string.replaceAll("\\$\\{member.brand\\}", member.brand);
			string = string.replaceAll("\\$\\{member.status\\}", member.getStatus());
		}
		if (contact != null) {
			string = string.replaceAll("\\$\\{contact.contactId\\}", contact.contactId + "");
			string = string.replaceAll("\\$\\{contact.memberId\\}", contact.memberId + "");
			string = string.replaceAll("\\$\\{contact.firstname\\}", contact.firstname);
			string = string.replaceAll("\\$\\{contact.surname\\}", contact.surname);
			string = string.replaceAll("\\$\\{contact.BHPhone\\}", contact.BHPhone);
			string = string.replaceAll("\\$\\{contact.AHPhone\\}", contact.AHPhone);
			string = string.replaceAll("\\$\\{contact.fax\\}", contact.fax);
			string = string.replaceAll("\\$\\{contact.identifier\\}", contact.identifier);
			string = string.replaceAll("\\$\\{contact.mobile\\}", contact.mobile);
			string = string.replaceAll("\\$\\{contact.email\\}", contact.email);
			string = string.replaceAll("\\$\\{contact.service\\}", contact.getService());
			string = string.replaceAll("\\$\\{contact.businessStreet\\}", contact.businessStreet);
			string = string.replaceAll("\\$\\{contact.businessCity\\}", contact.businessCity);
			string = string.replaceAll("\\$\\{contact.businessState\\}", contact.businessState);
			string = string.replaceAll("\\$\\{contact.businessCountry\\}", contact.businessCountry);
			string = string.replaceAll("\\$\\{contact.businessPostCode\\}", contact.businessPostCode);
			string = string.replaceAll("\\$\\{contact.postalStreet\\}", contact.postalStreet);
			string = string.replaceAll("\\$\\{contact.postalCity\\}", contact.postalCity);
			string = string.replaceAll("\\$\\{contact.postalState\\}", contact.postalState);
			string = string.replaceAll("\\$\\{contact.postalCountry\\}", contact.postalCountry);
			string = string.replaceAll("\\$\\{contact.postalPostCode\\}", contact.postalPostCode);
			string = string.replaceAll("\\$\\{contact.mailMemberId\\}", contact.mailMemberId + "");
			string = string.replaceAll("\\$\\{contact.mailContactId\\}", contact.mailContactId + "");
			string = string.replaceAll("\\$\\{contact.emailFaxContactId\\}", contact.emailFaxContactId + "");
			string = string.replaceAll("\\$\\{contact.status\\}", contact.status);
			string = string.replaceAll("\\$\\{contact.caption\\}", contact.getCaption());
		}
		if (orchard != null) {
			string = string.replaceAll("\\$\\{orchard.orchardId\\}", orchard.orchardId + "");
			string = string.replaceAll("\\$\\{orchard.memberId\\}", orchard.memberId + "");
			string = string.replaceAll("\\$\\{orchard.physicalLocation\\}", orchard.physicalLocation);
			string = string.replaceAll("\\$\\{orchard.postCode\\}", orchard.postCode);
			string = string.replaceAll("\\$\\{orchard.state\\}", orchard.state);
			string = string.replaceAll("\\$\\{orchard.emailContactId\\}", orchard.emailContactId);
			string = string.replaceAll("\\$\\{orchard.status\\}", orchard.status);
			string = string.replaceAll("\\$\\{orchard.location\\}", orchard.getLocation());
		}
		return string;
	}

	public static int _mailCode = 0;

	public static EmailVo getEmailVo(ContactVo contactVo) {
		return C.getEmailVo(contactVo, null);
	}

	public static EmailVo getEmailVo(ContactVo contactVo, OrchardVo orchardVo) {
		EmailVo emailVo = new EmailVo();

		emailVo.memberId = contactVo.memberId;
		emailVo.contactId = contactVo.contactId;
		if (orchardVo != null) emailVo.orchardId = orchardVo.orchardId;

		emailVo.memberHeader = contactVo.memberBusinessName + "";
		emailVo.contactHeader = contactVo.getCaption();
		if (orchardVo != null) emailVo.orchardHeader = orchardVo.getLocation();

		//emailVo.email = contactVo.email;
		if (!"".equals(contactVo.email)) {
			if (C._mailCode % 3 == 0) emailVo.email = "idm@mangoes.net.au";//"no_reply@mangoes.net.au";//contactVo.email
			if (C._mailCode % 3 == 1) emailVo.email = "mangomail001@yahoo.com";
			if (C._mailCode % 3 == 2) emailVo.email = "idm@mangoes.net.au";
			C._mailCode++;

			//\\emailVo.email = contactVo.email;
		} else emailVo.email = "";
		emailVo.statusEmail = "";

		if (!"".equals(contactVo.fax)) emailVo.fax = "0738461966";//contactVo.fax;
		else emailVo.fax = "";
		emailVo.statusFax = "";

		if (!"".equals(contactVo.mobile)) emailVo.sms = "0400808689";//contactVo.mobile
		else emailVo.sms = "";
		emailVo.statusSms = "";
		return emailVo;
	}
}
